﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
using System.Windows.Forms;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.report
{
    public partial class PublishReportForm : BaseForm
    {
        private static PublishReportForm rf;
        private static Object obj = new Object();
        public static PublishReportForm GetInstance()
        {
            if (rf == null || rf.IsDisposed)
            {
                lock (obj)
                {
                    if (rf == null || rf.IsDisposed)
                    {
                        rf = new PublishReportForm();
                    }
                }
            }
            return rf;
        }

        SiteCollection site = new SiteCollection();
        SqlServerOperate sql = new SqlServerOperate();
        string domain = "";

        private PublishReportForm()
        {
            InitializeComponent();
            site.LoadToComboBox(comboBox1);
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            button1.Enabled = false;
            new Thread(new ThreadStart(Start)).Start();
        }
        private void Start()
        {
            string d1 = GetControl("dt1", ControlValue.DateTimePicker).ToString();
            string d2 = GetControl("dt2", ControlValue.DateTimePicker).ToString();
            string cmd = "select CONVERT(varchar,a.dt,120) as 时间,a.title,a.word,a.url as 网址 from [发布总表] as a where a.dt between '" + d1 + "' and dateadd(d,1,'" + d2 + "')" + (string.IsNullOrEmpty(domain) ? "" : " and a.domain='" + domain + "'") + " order by id desc";
            SetControl("listview1", ControlValue.ListView, GetList(cmd));
            CreateListViewContentScriptMenu(listView1);
            SetControl("button1", ControlValue.Enable, true);
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            domain = site.GetDomain(comboBox1);
        }
        public List<ListViewItem> GetList(string cmd)
        {
            SqlDataReader re = sql.Query(cmd);
            int len = re.FieldCount;
            List<ListViewItem> list = new List<ListViewItem>();
            while (re.Read())
            {
                string[] k = new string[len];
                for (int i = 0; i < len; i++)
                {
                    k[i] = re[i].ToString();
                }
                list.Add(new ListViewItem(k));
            }
            re.Close();
            return list;
        }
    }
}
